import axios from 'axios';
const API_URL = import.meta.env.VITE_API_URL

const introOutroService = {
    /**
     * 获取可用的片头片尾视频列表
     * @param {string} type - 可选，筛选类型 ('opening' 或 'ending')
     * @returns {Promise} - 包含视频列表的 Promise
     */
    getIntroOutroVideos(type = null) {
        let url = `${API_URL}/search/intro_outro_videos`;
        if (type) {
            url += `?type=${type}`;
        }
        return axios.get(url)
            .then(response => response.data)
            .catch(error => {
                console.error('获取片头片尾视频列表失败:', error);
                throw error;
            });
    },

    /**
     * 上传视频文件作为片头或片尾
     * @param {File} file - 视频文件
     * @param {string} type - 视频类型 ('opening' 或 'ending')
     * @param {Object} metadata - 可选，附加元数据
     * @returns {Promise} - 上传结果的 Promise
     */
    uploadVideo(file, type, metadata = {}) {
        const formData = new FormData();
        formData.append('video_file', file);
        formData.append('video_type', type);

        if (metadata) {
            formData.append('meta_data', JSON.stringify(metadata));
        }

        return axios.post(`${API_URL}/tools/video`, formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
            .then(response => response.data)
            .catch(error => {
                console.error('上传视频失败:', error);
                throw error;
            });
    },

    /**
     * 处理文件夹中的视频，添加片头片尾
     * @param {string} folderPath - 视频所在文件夹路径
     * @param {string} outputFolder - 输出文件夹路径
     * @param {number} openingFileId - 片头视频ID
     * @param {number} endingFileId - 片尾视频ID
     * @param {string} transitionEffect - 过渡效果
     * @returns {Promise} - 处理结果的 Promise
     */
    processFolderIntroOutro(folderPath, outputFolder, openingFileId, endingFileId, transitionEffect = 'fade') {
        return axios.post(`${API_URL}/merge/process_folder_intro_outro`, {
            folder_path: folderPath,
            output_folder: outputFolder,
            opening_file_id: openingFileId,
            ending_file_id: endingFileId,
            transition_effect: transitionEffect
        })
            .then(response => response.data)
            .catch(error => {
                console.error('处理视频文件夹失败:', error);
                throw error;
            });
    },

    /**
     * 获取所有可用的转场效果
     * @returns {Promise} - 包含转场效果列表的 Promise
     */
    getTransitionEffects() {
        return axios.get(`${API_URL}/search/transition_effects`)
            .then(response => response.data)
            .catch(error => {
                console.error('获取转场效果失败:', error);
                throw error;
            });
    },

    /**
     * 设置默认视频
     * @param {number} videoId - 视频ID
     * @param {string} type - 视频类型 ('opening' 或 'ending')
     * @returns {Promise} - API响应结果
     */
    setDefaultVideo(videoId, type) {
        return axios.post(`${API_URL}/tools/set_default_video`, {
            video_id: videoId,
            video_type: type
        })
            .then(response => response.data)
            .catch(error => {
                console.error('设置默认视频失败:', error);
                throw error;
            });
    },

    /**
     * 删除视频
     * @param {number} videoId - 视频ID
     * @returns {Promise} - API响应结果
     */
    deleteVideo(videoId) {
        return axios.delete(`${API_URL}/tools/media/${videoId}`)
            .then(response => response.data)
            .catch(error => {
                console.error('删除视频失败:', error);
                throw error;
            });
    }
};

export default introOutroService;